//取消关注
const checkAll = async (max) => {

  // await delay(1000,()=>window.scrollTo(0,document.documentElement.scrollHeight));
  let ls = document.querySelectorAll("label.woo-checkbox-main");
  let ckcnt = 0;
  for (let index = 0; index < ls.length&&index<max; index++) {
    if (!ls[index].firstChild.checked) {
      ls[index].click();
      ckcnt++;
      await delay(500);
    }

  }
  return Promise.resolve(ckcnt);
}

let fullAction = async function () {
  try {
    let buttons = document.getElementsByTagName("button");
    let pl;
    for (let index = 0; index < buttons.length; index++) {
      if (buttons[index].textContent == ' 批量管理 ') {
        pl = buttons[index]; break;
      }
    }

    if (pl) {
      pl.click();
      await delay(1000);
    }

    let batch = 1;
    let now = new Date();
    console.log("开始执行取关当前,当前时间" + now);
    let sumcount = 0;

    while (1) {
      let startTime = Date.now();
      let checkCount = await checkAll(20);
      await delay(500, clickUnfollow);
      await delay(500, clickConfirm);
      let end = Date.now(); // 完成
      sumcount += checkCount;
      console.log("完成第" + batch + "次取关，本次取关" + checkCount + "人,共取关" + sumcount + "人用时" + (end - startTime) / 1000 + "秒");
      batch++;
      await delay(500);
    }
  }
  catch (err) {
    console.error("取关脚本停止运行", err);
  }
}
function clickUnfollow() {
  let buttons = document.getElementsByTagName("button");
  let qg;
  for (let index = 0; index < buttons.length; index++) {
    if (buttons[index].textContent == ' 取消关注 ') {
      qg = buttons[index]; break;
    }
  }

  qg.click();
}
function clickConfirm() {
  let buttons = document.getElementsByTagName("button");
  let qr;
  for (let index = 0; index < buttons.length; index++) {
    if (buttons[index].textContent == '\n        确认\n      ') {
      qr = buttons[index]; break;
    }
  }

  qr.click();
}



function delay(ms, fun) {
  // 你的代码
  if (fun) { fun(); }
  return new Promise(resolve => {
    setTimeout(() =>
      resolve(), ms);
  })
}



fullAction();